import Vue from 'vue'
import Vuex from 'vuex'
import {getlist} from '../api'
import pinyin from 'js-pinyin'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[],
    item:[],
    data:[]
  },
  mutations: {
    GET_LIST(state,payload){
      // 首字母转拼音
    let pin= payload.map(v=>{
        return ({
          ...v,
          pi:pinyin.getCamelChars(v.title)[0],
        })
      })
      // 排序
      let paipin=pin.sort((a,b)=>{
          return a.pi.charCodeAt()-b.pi.charCodeAt()
        })
      // // 去重
      let newpin=[...new Set(pin.map(v=>{
        return v.pi
      }))]
      // console.log(newpin)
     let arr=newpin.map(item=>{
        return ({
          index:item,
          children:paipin.filter(v=>v.pi===item)
        })
      })
    state.list=arr,
    state.item=newpin
    },
    SET_ID(state,payload){
      state.data=payload
      // console.log(state.data)
    }
  },
  actions: {
    async SET_LIST({commit}){
      const resp=await getlist()
      commit('GET_LIST',resp.data.data)
    }
  },
  modules: {
  }
})
